package user;

import constant.Constant;
import utils.PermissionException;
import book.Library;
import java.util.List;
import java.util.Scanner;

public class ProxyUser {
    private User realUser;
    private Library library;
    public User getRealUser() {
        return realUser;
    }

    public ProxyUser(User realUser) {
        this.realUser = realUser;
        library=Library.getInstance();
    }

    private void checkRealUserWhetherAdminUser(String msg) {
        if (!(realUser instanceof AdminUser)) {
            throw new PermissionException(msg);
        }
    }

    private void checkRealUserWhetherNormalUser(String msg) {
        if (!(realUser instanceof NormalUser)) {
            throw new PermissionException(msg);
        }
    }

    public void handleOperation(int choice) {
        if (realUser instanceof AdminUser) {
// 管理员操作
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowCount();
                    break;
                case Constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case Constant.CHECK_AND_REMOVE_OLD_BOOK:
                    checkAndRemoveOldBooks();
                    break;
                default:
                    System.out.println("无效的操作。");
            }
        } else if (realUser instanceof NormalUser) {
// 普通用户操作
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIT:
                    library.exit();
                case Constant.BORROWED_BOOK:
                    borrowBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_HISTORY_BOOK:
                    viewBorrowHistory();
                    break;
                default:
                    System.out.println("无效的操作。");
            }
        }
    }

        public void addBook () {
            checkRealUserWhetherAdminUser("普通用户没有权限上架图书");
            ((AdminUser) realUser).addBook();
        }
        //图书修改⽀持修改书名作者类别
        public void updateBook () {
        checkRealUserWhetherAdminUser("普通用户没有权限更新图书");
            ((AdminUser)realUser).updateBook();
        }
        //删除书籍

        public void removeBook () {
        checkRealUserWhetherAdminUser("普通用户没有权限删除图书");
        ((AdminUser)realUser).removeBook();
        }
        //统计每本书的借阅次数
        public void borrowCount () {
        checkRealUserWhetherAdminUser("普通用户没有权限查看图书的借阅次数");
        ((AdminUser)realUser).borrowCount();
        }
        //查询最受欢迎的前n本书

        public void generateBook () {
        checkRealUserWhetherAdminUser("普通用户没有权限查看最受欢迎的前k本书");
        ((AdminUser)realUser).generateBook();

        }
        //查看库存状态
        public void checkInventoryStatus () {
        checkRealUserWhetherAdminUser("普通用户没有权限查看库存状态");
        ((AdminUser)realUser).checkInventoryStatus();

        }
        //并移除上架超过⼀年的图书
        public void checkAndRemoveOldBooks () {
            checkRealUserWhetherAdminUser("普通用户没有权限移除上架超过一年的图书");
            ((AdminUser)realUser).checkAndRemoveOldBooks();
        }
        //--------------------------------普通相关⽅法-----------------------------

        //借阅图书
        public void borrowBook () {
            checkRealUserWhetherNormalUser("管理员请以普通用户的方式借阅图书");
            ((NormalUser)realUser).borrowBook();
        }
        //归还图书
        public void returnBook () {
            checkRealUserWhetherNormalUser("管理员请以普通用户的方式归还图书");
            ((NormalUser)realUser).returnBook();
        }

        //查看个⼈借阅情况
        public void viewBorrowHistory () {
            checkRealUserWhetherNormalUser("管理员请以普通用户的方式查看个人当前借阅情况");
            ((NormalUser)realUser).viewBorrowBooks();
        }
}

